package com.study.business.controller;

//import com.alibaba.fescar.core.context.RootContext;
//import com.alibaba.fescar.spring.annotation.GlobalTransactional;

import com.study.business.feign.OrderService;
import com.study.business.feign.StorageService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author
 */
@Slf4j
@RestController
public class HomeController {


    private static final String SUCCESS = "SUCCESS";
    private static final String FAIL = "FAIL";
    private static final String USER_ID = "U100001";
    private static final String COMMODITY_CODE = "C00321";
    private static final int ORDER_COUNT = 2;

    private final RestTemplate restTemplate;
    private final OrderService orderService;
    private final StorageService storageService;

    /**
     * 构造函数注入
     * @param restTemplate
     * @param orderService
     * @param storageService
     */
    public HomeController(RestTemplate restTemplate, OrderService orderService, StorageService storageService) {
        this.restTemplate = restTemplate;
        this.orderService = orderService;
        this.storageService = storageService;
    }

    @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
    @RequestMapping(value = "/fescar/rest", method = RequestMethod.GET, produces = "application/json")
    public String rest() {
        log.info("RECV:");
        String url = "http://127.0.0.1:18082/storage/" + COMMODITY_CODE + "/" + ORDER_COUNT;
        String result = restTemplate.getForObject(url, String.class);
        log.info("Result1:{}",result);
        if (!SUCCESS.equals(result)) {
            throw new RuntimeException();
        }

        url = "http://127.0.0.1:18083/order";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("userId", USER_ID);
        map.add("commodityCode", COMMODITY_CODE);
        map.add("orderCount", ORDER_COUNT + "");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        result = response.getBody();
        log.info("Result2:{}",result);
        if (SUCCESS.equals(result)) {
            return SUCCESS;
        }

        throw new RuntimeException();
    }


    @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
    @RequestMapping(value = "/fescar/feign", method = RequestMethod.GET, produces = "application/json")
    public String feign() {
        log.info("business Service Begin ... xid: " + RootContext.getXID());
        String result = storageService.storage(COMMODITY_CODE, ORDER_COUNT);

        if (!SUCCESS.equals(result)) {
            throw new RuntimeException();
        }

        result = orderService.order(USER_ID, COMMODITY_CODE, ORDER_COUNT);
        if (!SUCCESS.equals(result)) {
            throw new RuntimeException();
        }

        return SUCCESS;
    }

}